草庐IT

java - 自定义 Spring Bean 参数

全部标签

ruby-on-rails - 事件管理员 CSV 导出自定义查询范围

我正在使用活跃的管理员导出CSV选项。它返回与特定表相关的所有值。我只想要特定月份的报告。有人能帮忙吗? 最佳答案 您可以编写自己的csv导出器collection_action:download_report,:method=>:getdousers=User.where('created_at>=?',Date.today-1.month)csv=CSV.generate(encoding:'Windows-1251')do|csv|#addheaderscsv:download_report))endindex:downloa

ruby - RSpec 自定义可区分匹配器

我在RSpec中有一个自定义匹配器,它忽略空格/换行符,只匹配内容:RSpec::Matchers.define:be_matching_contentdo|expected|matchdo|actual|actual.gsub(/\s/,'').should==expected.gsub(/\s/,'')enddiffableend我可以这样使用它:body="somedata\nmoredata"body.shouldbe_matching_content("somedata\nmorewrongdata")但是,当测试失败时(如上面的测试),diff输出看起来不太好:-somed

ruby - 为什么我们将 0 作为参数传递给 "exit"?

在艰难地学习ruby这本书中,我找到了退出程序的语法:Process.exit(0)为什么参数0被传递到这里的exit方法中,即使我传递另一个整数或不传递任何参数它都有效?0有什么意义? 最佳答案 这是一个“退出代码”。此退出代码在某些情况下具有特殊含义(参见示例http://tldp.org/LDP/abs/html/exitcodes.html)你可以传递任何你想要的,如果后面的代码没有被捕获,这将没有任何效果。这里的“0”代表“一切正常!” 关于ruby-为什么我们将0作为参数传递

ruby-on-rails - 获取 URL 字符串参数?

我的数据库中有这个URL,在“位置”字段中:http://www.youtube.com/watch?v=xxxxxxxxxxxxxxxxxxx我可以通过@object.location获取,但是如何获取v的值呢?我的意思是,从URL字符串中获取"xxxxxxxxxxxx"? 最佳答案 require'uri'require'cgi'#useURI.parsetoparsetheURLintoitsconstituentparts-host,port,querystring..uri=URI.parse(@object.locati

ruby - 在嵌套对象中使用自定义 to_json 方法

我有一个使用Ruby标准库中的Set类的数据结构。我希望能够将我的数据结构序列化为JSON字符串。默认情况下,Set序列化为数组:>>s=Set.new[1,2,3]>>s.to_json=>"[1,2,3]"这很好,直到您尝试反序列化它。所以我定义了一个自定义的to_json方法:classSetdefto_json(*a){"json_class"=>self.class.name,"data"=>{"elements"=>self.to_a}}.to_json(*a)enddefself.json_create(o)newo["data"]["elements"]endend哪个

ruby - sinatra路由中的几个可选参数

我需要Sinatra路由以下列方式运行:GET/list/20/10#Get20itemswithoffset10GET/list/20#Get20itemswithdefaultoffsetGET/list#Getdefaultnumberofitemswithdefaultoffset我明白,我可能会将值作为查询传递:GET/list?limit=20&offset=10但我想如上所述传递它们。我很确定有一种方法可以向Sinatra/Padrino解释我想做什么,但我目前完全被困住了。我试过:get:list,:map=>'/list',:with=>[:limit,:offset

ruby-on-rails - 使用 get 和 delete 运行 Rspec 测试时获取错误数量的参数(2 个为 0)

这应该是一个简单的问题,就是找不到导致测试失败的原因。运行rspec时,我不断收到以下错误。但是在评论“发送”方法之后,一切正常。1)MessagesGET/messagesworks!(nowwritesomerealspecs)Failure/Error:gettarget_app_messages_path(@message.target_app.id)ArgumentError:wrongnumberofarguments(2for0)#./app/controllers/messages_controller.rb:37:in`send'路线.rbresources:targ

ruby-on-rails - 如何使用 FactoryGirl 发送参数(而不是手动将参数作为散列发送)?

我有以下有效的rspec测试:it"redirectstothecreatedapi_key"dopost:create,:api_key=>{:api_identifier=>"asdfadsf",:verification_code=>"12345"}response.shouldredirect_to(ApiKey.last)#(oranyothertestfunction)end但我使用Factorygirl,所以我不必手动创建api_key。如何复制上述功能,但使用factorygirl?使用:it"redirectstothecreatedapi_key"dotest=Fa

ruby-on-rails - 参数错误 : wrong number of arguments (1 for 2)

我是Rails、MVC和CRUD的新手,我正在尝试使用更新方法来更改帖子的投票数量。我的PostsController更新方法中有以下代码:defupdate@post=Post.find(params[:id])ifparams[:vote]=='up'@post.update_column(:ups=>@post[:ups]+1)elsifparams[:vote]=='down'@post.update_column(:downs=>@post[:downs]+1)endflash[:notice]="Thanksforvoting!Thishelpsusdetermineimp

ruby - splat 参数后的可选参数

这是我的程序:defcalculate(*numbers,options={})add(numbers)ifoptions[:add]subtract(numbers)ifoptions[:add]==falseenddefadd(*numbers)numbers.reduce(:+)enddefsubtract(*numbers)numbers.reduce(:-)endpcalculate(1,2)在第一行,它在提示tests.rb:1:syntaxerror,unexpected'=',expecting')'defcalculate(*numbers,options={})__